//
//  MeAddressViewController.m
//  RainbowFM
//
//  Created by jv on 14-12-9.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "MeAddressViewController.h"
#import "MeAddressCell.h"
#import "UserModel.h"
#import "MeFillAddressViewController.h"

@interface MeAddressViewController (){
    NSMutableArray *listArray;
}

@end

@implementation MeAddressViewController

-(void)fetchListData{
    [listArray removeAllObjects];
    [[UserModel sharedModel] fetchUserAddressListWithPage:1 result:^(BOOL success, NSArray *array) {
        [listArray addObjectsFromArray:array];
        [self.tableView reloadData];
    }];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self fetchListData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    listArray = [NSMutableArray array];
    self.tableView.tableFooterView = [UIView new];
    
    self.title = @"收货地址";
    
//    [self fetchListData];
    
}

#pragma mark - table datasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return listArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MeAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MeAddressCell class])];

    AddressListItem *item = listArray[indexPath.row];
    
    cell.nameLabel.text = item.deliName;
    cell.phoneLabel.text = item.deliTel;
    cell.addressLabel.text = item.deliAddr;
    
    
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    MeAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MeAddressCell class])];
    
    AddressListItem *item = listArray[indexPath.row];
    
    cell.nameLabel.text = item.deliName;
    cell.phoneLabel.text = item.deliTel;
    cell.addressLabel.text = item.deliAddr;
    
    [cell layoutIfNeeded];
    
    CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    
    return height + 1;
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        AddressListItem *item = listArray[indexPath.row];
        [[UserModel sharedModel] removeAddressWithDeliID:item.deliId result:^(BOOL success) {
            if (success) {
                [listArray removeObjectAtIndex:indexPath.row];
                [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            }
        }];
        
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
    }
}

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}


-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

#pragma mark - table delegate



#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    if ([[segue identifier] isEqualToString:@"addAddr"]) {
        [[segue destinationViewController] setAddressType:0];
    }
    else if([[segue identifier] isEqualToString:@"editAddr"]){
            NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
            AddressListItem *item = listArray[indexPath.row];
            [[segue destinationViewController] setAId:item.deliId];
            [[segue destinationViewController] setAddressType:1];
    }
}


@end
